﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static 卷书.Global;

namespace 卷书.Utils
{
    public static class FileHelper
    {
        public static Window GetWindowByName(string name)
        {
            foreach (Window window in App.Current.Windows)
            {
                if (window.GetType().Name == name) return window;
            }
            return null;
        }

        /// <summary>
        /// 判断拖入的是文件夹还是文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool IsFile(string path)
        {
            try
            {
                FileAttributes attr = File.GetAttributes(path);
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    return false;
                else
                    return true;
            }
            catch
            {
                return true;
            }
        }

        public static bool TryOpenSelectPath(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    Process.Start("explorer.exe", "/select, \"" + path + "\"");
                    return true;
                }

                else
                {
                    StatusMessage = new StringBuilder("文件不存在");
                    return false;
                }

            }
            catch (Exception ex)
            {
                StatusMessage = new StringBuilder(ex.Message);
                Logger.LogF(ex);
                return false;
            }
        }

        public static bool TryOpenFile(string filename)
        {
            try
            {
                if (File.Exists(filename))
                {
                    Process.Start("\"" + filename + "\"");
                    return true;
                }

                else
                {
                    StatusMessage = new StringBuilder("文件不存在");
                    return false;
                }


            }
            catch (Exception ex)
            {
                StatusMessage = new StringBuilder(ex.Message);
                Logger.LogF(ex);
                return false;
            }
        }
    }


}
